package byj.com.meishi;

import android.content.ContentValues;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.Window;
import android.widget.MediaController;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.VideoView;

import byj.com.meishi.bean.Shoucang;
import byj.com.meishi.dao.ShouCangDao;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.QueryListener;
import cn.bmob.v3.listener.SaveListener;

/**
 * Created by Administrator on 2016/9/22 0022.
 */
public class MovieActivity extends AppCompatActivity{

    private VideoView vv;
    private TextView movedecl;
    private String video;
    private String caption;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.movie_layout);
        Intent intent = getIntent();
        video = intent.getStringExtra("video");
        caption = intent.getStringExtra("caption");

        vv = (VideoView) findViewById(R.id.vv);
        movedecl = (TextView) findViewById(R.id.movie_decl);
        vv.setMediaController(new MediaController(this));
        vv.setVideoURI(Uri.parse(video));
        vv.start();
        movedecl.setText(caption);
    }

    public void shoucang(View view) {
        final ShouCangDao dao = new ShouCangDao(MovieActivity.this);
        //查找Person表里面id为6b6c11c537的数据
        BmobQuery<Shoucang> bmobQuery = new BmobQuery<Shoucang>();
        bmobQuery.getObject("" + video, new QueryListener<Shoucang>() {
            @Override
            public void done(Shoucang shoucang, BmobException e) {
                if(e==null){

                    Toast.makeText(MovieActivity.this,"已收藏,不必再收藏了",Toast.LENGTH_SHORT).show();

                }else {
                    Shoucang shou=new Shoucang();
                    shou.setName(caption);
                    shou.setUrl(video);
                    shou.save(new SaveListener<String>() {
                        @Override
                        public void done(String s, BmobException e) {
                            if(e==null){

                                ContentValues values = new ContentValues();
                                values.put("url",video);
                                values.put("caption",caption);
                                long insert = dao.insert("collect", values);
                                if(insert>0){
                                    Toast.makeText(MovieActivity.this,"收藏成功",Toast.LENGTH_SHORT).show();
                                }

                            }else {
                                Toast.makeText(MovieActivity.this,"收藏失败：" + e.getMessage(),Toast.LENGTH_SHORT).show();
                            }
                        }
                    });
                }
            }
        });
    }
}
